package Test.contorller;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import Test.httpModel.Users;
import Test.service.UserService;

import common.controller.BaseController;
import common.httpModel.EasyuiDataGrid;
import common.httpModel.EasyuiDataGridJson;
import common.httpModel.Json;
import common.util.ExceptionUtil;

@Controller
@RequestMapping("/testController")
public class TestController extends BaseController{
	
	private static final Logger logger = Logger.getLogger(TestController.class);
	
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserService tUserService) {
		this.userService = tUserService;
	}

	@RequestMapping(params="datagrid")
	@ResponseBody
	public EasyuiDataGridJson datagrid(EasyuiDataGrid gd,Users users){
		System.out.println("test");
		System.out.println("test");
		return userService.showDatagrid(gd,users);
	}
	
	@RequestMapping(params="addUser")
	@ResponseBody
	public Json addUser(Users users){
		Json j=new Json();
		try {
			users=userService.addUser(users);
			j.setObj(users);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("用户名已存在！");
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}

		return j;
	}
	@RequestMapping(params="updateUser")
	@ResponseBody
	public Json updateUser(Users user){
		Json j=new Json();
		try {
			user=userService.update(user);
			j.setObj(user);
			j.setMsg("修改成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("修改失败！");
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
		
		return j;
	}
	
	@RequestMapping(params="del")
	@ResponseBody
	public Json del(String ids){
		Json j=new Json();
		boolean falg=userService.del(ids);
		if(falg){
			j.setMsg("刪除成功！");
			j.setSuccess(true);	
		}else{
			j.setMsg("刪除失敗！");
		}
		
		return j;
	}

}
